
function VehiclesView(currentWindow) {
	var self = Ti.UI.createView({
		backgroundColor:'white'
	});
	
	// Create table with vehicle rows
	var table = Ti.UI.createTableView({
		data:GetVehicleArray()
	});
	
	self.add(table);
	
	// Add behavior
	table.addEventListener('click', function(e) {
		
		self.fireEvent('vehicleSelected', {
			vehicleId:e.rowData.vehicleId,
			vehicleName:e.rowData.vehicleName,
			averageFuelEconomy:e.rowData.averageFuelEconomy,
			averageFuelConsumption:e.rowData.averageFuelConsumption
		});
		
	});
	
	self.addEventListener('updateTableEvent', function(e) {
		table.setData(GetVehicleArray());
	});
	
	ModifyNavBar(self, currentWindow);
	
	return self;
};

function GetVehicleArray() {
	var data = [];
	var Vehicle = require('/models/Vehicle');
	var vehicles = Vehicle.findAll();
	for(var i=0; i<vehicles.length; i++) {
	
		var vehicle = vehicles[i];
		
		var row = Ti.UI.createTableViewRow();
		
		row.vehicleId = vehicle.id;
		row.vehicleName = vehicle.make + ' ' + vehicle.model;
		row.averageFuelConsumption = vehicle.fillupUnitsPerDistance;
		row.averageFuelEconomy = vehicle.distancePerFillupUnit;
		
		// If vehicle photo is null, we set it to something invalid, causing a
		// generic icon to be displayed
		var usesGenericPhoto = false;
		if(!vehicle.photo) {
			vehicle.photo = 'invalid/photo';
			usesGenericPhoto = true;
			vehicle.isPhotoWide = 1; // Invalid photo is wide
		}
		
		// Height of row
		row.height = 90;
		
		// Create an ImageView of the vehicle photo
		// The generic icon has white edges so to line it up with the photo thumbnail we
		// have to adjust the width and left position a bit
		var anImageView;
		if(usesGenericPhoto) {
			anImageView = Ti.UI.createImageView({
				image : 'car.png',
				width : 80,
				height: 72,
				top : 4,
				hires: true,
				left : 5
			});
		} else {
			
			anImageView = Ti.UI.createImageView({
				image : vehicle.photo,
				width : 80,
				height : 80,
				hires: true,
				top : 4,
				left : 5
			});
		}
		
		// Add to the parent view.
		row.add(anImageView);
	
		var carName = Ti.UI.createLabel({
			color:'#576996',
			font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'},
			left:94,
			top:5,
			height:30,
			width:190,
			text:row.vehicleName
		});
	
		row.add(carName);
		
		if(vehicle.distancePerFillupUnit && !isNaN(vehicle.distancePerFillupUnit)) {
			var fuelInfo = Ti.UI.createLabel({
				color:'#999',
				font:{fontSize:12,fontWeight:'bold', fontFamily:'Arial'},
				left:94,
				top:35,
				height:15,
				width:190,
				text:'Avg: ' + vehicle.fillupUnitsPerDistance + ' L/100km, ' + 
					vehicle.distancePerFillupUnit + ' km/L'
			});
			
			row.add(fuelInfo);
		}
	
		if(vehicle.distancePerFillupUnitBest && !isNaN(vehicle.distancePerFillupUnitBest)
			&& isFinite(vehicle.distancePerFillupUnitBest)) {
			var fuelInfo = Ti.UI.createLabel({
				color:'#999',
				font:{fontSize:12,fontWeight:'bold', fontFamily:'Arial'},
				left:94,
				top:50,
				height:15,
				width:190,
				text:'Best: ' + vehicle.fillupUnitsPerDistanceBest + ' L/100 km, ' + 
					vehicle.distancePerFillupUnitBest + ' km/L'
			});
			
			row.add(fuelInfo);
		}
		
		if(vehicle.lastFillup) {
			var lastFillupDateObj = new Date(vehicle.lastFillup*1000);
			var avgCostPerFillup = Ti.UI.createLabel({
				color:'#999',
				font:{fontSize:12,fontWeight:'bold', fontFamily:'Arial'},
				left:94,
				top:65,
				height:15,
				width:190,
				text:'Last filled ' + String.formatDate(lastFillupDateObj) 
					 + ' (' + vehicle.lastFillupUnits.toFixed(2) + ' L)'
			});
		
			row.add(avgCostPerFillup);
		}
		
		var arrowButton = Ti.UI.createButton({
			style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
			right:5
		});
		
		row.add(arrowButton);
		
		data.push(row);
	}
	
	return data;
}

/**
 * Modify navigation bar to show an add-icon
 */
function ModifyNavBar(self, currentWindow) {
	var addNewEntryButton = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.ADD
	});
	
	addNewEntryButton.addEventListener('click', function()
	{
		self.fireEvent('addNewVehicleEvent');
	});

	currentWindow.rightNavButton = addNewEntryButton;
}

module.exports = VehiclesView;
